<!-- 维修单号设置 -->
<template>
  <div>
    <pageHeader :title="title" :bread-crumb="breadCrumb" :show-back="true">
      <template #buts>
        <a-button type="primary" @click="save">保存</a-button>
        <a-button @click="cancel">取消</a-button>
      </template>
    </pageHeader>
    <CodeSettingNew ref="codeSettingRef" :form="form"></CodeSettingNew>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { useRouter } from 'vue-router';
  import pageHeader from '@/components/page-header/index.vue';
  import {
    postEquipmentNum,
    getEquipmentNum,
  } from '@/api/equipment-management/base-setting/index';
  import { cloneDeep } from 'lodash';

  const router = useRouter();
  const title: string = '维修单号设置';

  const breadCrumb: string[] = ['设备管理', '基础设置', '维修单号设置'];

  let codeSettingRef = ref(); // 储存编号设置ref
  let form = ref<any>({
    preStruct: {
      description: '',
      length: 1,
      name: 'cjs',
      isEnabled: 1,
    },
    time: {
      isEnabled: 2,
      way: '',
      content: undefined,
    },
    length: 1,
    delimiter: '',
    code: '',
  });
  const getEquipmentNumData = async () => {
    const res = await getEquipmentNum({ category: 23 });
    if (res.timeStamp) {
      let time = {
        way: 0,
        isEnabled: 1,
        content: res.timeStamp,
      };
      if (res.timeStamp.length === 4) {
        time.way = 1;
      } else if (res.timeStamp.length === 6) {
        time.way = 2;
      } else if (res.timeStamp.length === 8) {
        time.way = 3;
      }
      res.time = time;
      delete res.timeStamp;
    }
    res.preStruct = JSON.parse(res.preStruct);
    codeSettingRef.value.currForm = res;
  };
  getEquipmentNumData();

  // 保存按钮
  const save = async () => {
    let newCurrForm = cloneDeep(codeSettingRef.value.currForm);
    if (codeSettingRef.value.currForm.time.isEnabled === 1) {
      newCurrForm.timeStamp = codeSettingRef.value.currForm.time.content;
      delete newCurrForm.time;
    } else {
      delete newCurrForm.time;
    }
    newCurrForm.preStruct = JSON.stringify(newCurrForm.preStruct);
    newCurrForm.categoryId = 23;
    await postEquipmentNum({ ...newCurrForm });
    getEquipmentNumData();
  };
  // 取消按钮
  const cancel = () => router.back();
</script>

<style scoped lang="less">
  .number-setting {
    padding: 20px;
  }
</style>
